//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ProxyIbor.h"
using namespace Cephei::QL::Experimental::Coupons;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Currency.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Coupons::CProxyIbor::CProxyIbor (String^ familyName, Cephei::QL::Times::IPeriod^ tenor, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Times::ICalendar^ fixingCalendar, QL::Times::BusinessDayConventionEnum convention, Boolean endOfMonth, Cephei::QL::Times::IDayCounter^ dayCounter, Cephei::QL::IQuote^ gearing, Cephei::QL::Indexes::IIborIndex^ iborIndex, Cephei::QL::IQuote^ spread) : CIborIndex(CProxyIbor::typeid)
{
    CPeriod^ _Ctenor;
    CCurrency^ _Ccurrency;
    CCalendar^ _CfixingCalendar;
    CDayCounter^ _CdayCounter;
    CQuote^ _Cgearing;
    CIborIndex^ _CiborIndex;
    CQuote^ _Cspread;
    try
    {
#ifdef HANDLE
        _phProxyIbor = NULL;
#endif
        std::string _familyName = (std::string)ValueHelper::Convert (familyName); //d
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        _Ccurrency = safe_cast<CCurrency^> (currency);
        _Ccurrency->Lock();
        QuantLib::Currency& _currency = static_cast<QuantLib::Currency&> (_Ccurrency->GetReference ()); 
        _CfixingCalendar = safe_cast<CCalendar^> (fixingCalendar);
        _CfixingCalendar->Lock();
        QuantLib::Calendar& _fixingCalendar = static_cast<QuantLib::Calendar&> (_CfixingCalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _convention = (QuantLib::BusinessDayConvention)convention ;
        bool _endOfMonth = (bool)ValueHelper::Convert (endOfMonth); //d
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _Cgearing = safe_cast<CQuote^> (gearing);
        _Cgearing->Lock();
        Handle<QuantLib::Quote>& _gearing = static_cast<Handle<QuantLib::Quote>&> (_Cgearing->GetHandle ()); 
        _CiborIndex = safe_cast<CIborIndex^> (iborIndex);
        _CiborIndex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _iborIndex = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_CiborIndex->GetShared ()); 
        _Cspread = safe_cast<CQuote^> (spread);
        _Cspread->Lock();
        Handle<QuantLib::Quote>& _spread = static_cast<Handle<QuantLib::Quote>&> (_Cspread->GetHandle ()); 
        _ppProxyIbor = new boost::shared_ptr<QuantLib::ProxyIbor> (new QuantLib::ProxyIbor ( _familyName,  _tenor,  _settlementDays,  _currency,  _fixingCalendar,  _convention,  _endOfMonth,  _dayCounter,  _gearing,  _iborIndex,  _spread ));
        SetIborIndex (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppProxyIbor));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ccurrency != nullptr) _Ccurrency->Unlock();
        if (_CfixingCalendar != nullptr) _CfixingCalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_Cgearing != nullptr) _Cgearing->Unlock();
        if (_CiborIndex != nullptr) _CiborIndex->Unlock();
        if (_Cspread != nullptr) _Cspread->Unlock();
    }
}
Cephei::QL::Experimental::Coupons::CProxyIbor::CProxyIbor (boost::shared_ptr<QuantLib::ProxyIbor>& childNative, Object^ owner) : CIborIndex(CProxyIbor::typeid)
{
#ifdef HANDLE
	_phProxyIbor = NULL;
#endif
	_ppProxyIbor = &childNative;
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppProxyIbor));
}
Cephei::QL::Experimental::Coupons::CProxyIbor::CProxyIbor (QuantLib::ProxyIbor& childNative, Object^ owner) : CIborIndex(CProxyIbor::typeid)
{
#ifdef HANDLE
	_phProxyIbor = NULL;
#endif
	_ppProxyIbor = new boost::shared_ptr<QuantLib::ProxyIbor> (&childNative);
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppProxyIbor));
    _ProxyIborOwner = owner;
    _IborIndexOwner = owner;
}

Cephei::QL::Experimental::Coupons::CProxyIbor::CProxyIbor (CProxyIbor^ copy) : CIborIndex(CProxyIbor::typeid)
{
#ifdef HANDLE
	_phProxyIbor = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppProxyIbor = new boost::shared_ptr<QuantLib::ProxyIbor> (copy->GetShared());
        _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppProxyIbor));
    }
}
Cephei::QL::Experimental::Coupons::CProxyIbor::CProxyIbor (PLATFORM::Type^ t) : CIborIndex(CProxyIbor::typeid)
{
#ifdef HANDLE
	_phProxyIbor = NULL;
#endif
	if (!t->IsSubclassOf(CProxyIbor::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Coupons::CProxyIbor::CProxyIbor (QuantLib::Handle<QuantLib::ProxyIbor>& childNative, Object^ owner)  : CIborIndex(CProxyIbor::typeid)
{
	_phProxyIbor = &childNative;
	_ppProxyIbor = &static_cast<boost::shared_ptr<QuantLib::ProxyIbor>>(childNative.currentLink());
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppProxyIbor));
    _ProxyIborOwner = owner;
}
Cephei::QL::Experimental::Coupons::CProxyIbor::CProxyIbor (QuantLib::Handle<QuantLib::ProxyIbor> childNative)  : CIborIndex(CProxyIbor::typeid)
{
	_phProxyIbor = &childNative;
	_ppProxyIbor = &static_cast<boost::shared_ptr<QuantLib::ProxyIbor>>(childNative.currentLink());
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppProxyIbor));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Coupons::CProxyIbor::CProxyIbor (QuantLib::ProxyIbor childNative)  : CIborIndex(CProxyIbor::typeid)
{
#ifdef HANDLE
	_phProxyIbor = NULL;
#endif
	_ppProxyIbor = new boost::shared_ptr<QuantLib::ProxyIbor> (new QuantLib::ProxyIbor (childNative));
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppProxyIbor));
}
#endif

Cephei::QL::Experimental::Coupons::CProxyIbor::~CProxyIbor ()
{
    if (_ppProxyIbor != NULL)
    {
	    delete _ppProxyIbor;
        _ppProxyIbor = NULL;
    }
}
Cephei::QL::Experimental::Coupons::CProxyIbor::!CProxyIbor ()
{
    if (_ppProxyIbor != NULL)
    {
	    delete _ppProxyIbor;
    }
}
QuantLib::ProxyIbor& Cephei::QL::Experimental::Coupons::CProxyIbor::GetReference ()
{
    if (_ppProxyIbor == NULL) throw REFNEW NativeNullException ();
	return **_ppProxyIbor;
}
boost::shared_ptr<QuantLib::ProxyIbor>& Cephei::QL::Experimental::Coupons::CProxyIbor::GetShared ()
{
    if (_ppProxyIbor == NULL) throw REFNEW NativeNullException ();
	return *_ppProxyIbor;
}
QuantLib::ProxyIbor* Cephei::QL::Experimental::Coupons::CProxyIbor::GetPointer ()
{
    if (_ppProxyIbor == NULL) throw REFNEW NativeNullException ();
	return &**_ppProxyIbor;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ProxyIbor>& Cephei::QL::Experimental::Coupons::CProxyIbor::GetHandle ()
{
	if (_phProxyIbor == NULL)
	{
		_phProxyIbor = new Handle<QuantLib::ProxyIbor> (*_ppProxyIbor);
	}
	return *_phProxyIbor;
}
#endif
bool Cephei::QL::Experimental::Coupons::CProxyIbor::HasNative () 
{
	return (_ppProxyIbor != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Coupons::IProxyIbor^ Cephei::QL::Experimental::Coupons::CProxyIbor_Factory::Create (String^ familyName, Cephei::QL::Times::IPeriod^ tenor, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Times::ICalendar^ fixingCalendar, QL::Times::BusinessDayConventionEnum convention, Boolean endOfMonth, Cephei::QL::Times::IDayCounter^ dayCounter, Cephei::QL::IQuote^ gearing, Cephei::QL::Indexes::IIborIndex^ iborIndex, Cephei::QL::IQuote^ spread)
{
    return REFNEW CProxyIbor ( familyName,  tenor,  settlementDays,  currency,  fixingCalendar,  convention,  endOfMonth,  dayCounter,  gearing,  iborIndex,  spread);
}
